<?php
/**
 * 
 */
gCore::includeCoreObject('app');
/**
 *
 */
gCore::includePackageFile('dbo', 'Table');

class gDboMysql extends gDboAdapterAbstract implements gDboAdapterStructure
{
	const cquote = "'";
	/**
	 * (non-PHPdoc)
	 * @see gDboAdapterAbstract::connect()
	 */
	public function connect($options)
	{
        if( gApp::get('dbopersistent'))
        {
            $this->_connection = mysql_pconnect($options['host'],$options['user'],$options['password']);
        }
        else
        {
             $this->_connection = mysql_connect($options['host'],$options['user'],$options['password']);
        }
		if( !$this->_connection)
                {
                    throw new gException('Cannot connect to Mysql server', gError::CORE);
                }
		mysql_select_db($options['db'],$this->_connection);
	}
	/**
	 * (non-PHPdoc)
	 * @see gDboAdapterAbstract::disconnect()
	 */
	public function disconnect()
	{
		mysql_close($this->_connection);
	}
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $query
	 * @param unknown_type $fetchtype
	 */
	public function fetch($query,$fetchtype = null)
	{
		if ($fetchtype == gDboQuery::FETCH_ALL)
		{
			$array = null;
			while ( $row = mysql_fetch_assoc($query))
			{
				$array[] = $row;
			}
			mysql_free_result($query);
			return $array;
		}
		else if ($fetchtype == gDboQuery::FETCH_ASSOC)
		{
			return mysql_fetch_assoc($query);
		}
		else if ($fetchtype == gDboQuery::FETCH_LIST)
		{
			$array = null;
			while ( $row = mysql_fetch_row($query))
			{
				$array[] = $row[0];
			}
			mysql_free_result($query);
			return $array;	
		}
		else if ($fetchtype == gDboQuery::FETCH_ONE)
		{
			$row = mysql_fetch_row($query);
			mysql_free_result($query);
			return $row[0];
		}
		else if ($fetchtype == gDboQuery::FETCH_ROW)
		{
			return mysql_fetch_row($query);
		}
		else if ($fetchtype == gDboQuery::FETCH_OBJECT)
		{
			return mysql_fetch_object($query);
		}
		
	}
	/**
	 * (non-PHPdoc)
	 * @see gDboAdapterAbstract::execute()
	 */
	public function execute($sql,$result = null)
	{
		if (is_object($sql))
		{
			$sql = $sql->toStirng();
		}
		$query = mysql_query($sql,$this->_connection);
		
		if ($result != null)
		{
			return $this->fetch($query,$result);	
		}
		return true;
	}
    /**
     *
     * @param <type> $tablename 
     */
    public function  getTable($tablename)
    {
    
    }

}